#include "Log.h"

using namespace std;

Log::Log(void) throw(string)
{
	char path[25];
	GetDate();
	sprintf(path, "./Logs/%s.log", buf);
	fout.open(path, fstream::out | fstream::in | fstream::app);
	if(fout.fail())
	{
		throw string("Nie udalo się zaincjalizowac klase logujaca. Upewnij się że istnieje folder Logs");
	}
	GetTime();
	fout << buf << ": Zainicjowano klasę logujaca" << endl;
}


Log::~Log(void)
{
	GetTime();
	fout << buf << ": Klasa logujaca kończy działanie" << endl;
	fout << "-----------------------------------------------" << endl << endl;
	fout.close();
}


void Log::Info(const char * msg)
{
	GetTime();
	fout << "Info " << buf << ": " << msg << endl;
}

void Log::Warning(const char * msg)
{
	GetTime();
	fout << "Warning " << buf << ": " << msg << endl;
}

void Log::Error(const char * msg)
{
	GetTime();
	fout << "Error " << buf << ": " << msg << endl;
}


void Log::PutMsg(char * msg)
{
	GetTime();
	fout << buf << ": " << msg << endl;
}

void Log::GetTime(void)
{
	time_t sectime;
	struct tm * timeinfo;

	time(&sectime);
	timeinfo = localtime(&sectime);

	sprintf(buf, "%d:%d:%d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
}

void Log::GetDate(void)
{
	time_t sectime;
	struct tm * timeinfo;

	time(&sectime);
	timeinfo = localtime(&sectime);

	sprintf(buf, "%d-%d-%d", timeinfo->tm_mday, timeinfo->tm_mon+1, timeinfo->tm_year+1900);

}
